<%@ page language="java" import="java.util.*,java.sql.*,connBean.*,java.text.SimpleDateFormat,java.util.Date" pageEncoding="utf-8"%>
<%@page import="com.jspsmart.upload.*"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<jsp:useBean id="db" class="connBean.ConnDB" scope="page">
</jsp:useBean>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>保存页面</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
<% 
  request.setCharacterEncoding("utf-8");
  SmartUpload upload =new SmartUpload();
  upload.initialize(pageContext);
  upload.setMaxFileSize(2000000000);
  upload.setAllowedFilesList("jpg,JPG,gif");
  upload.setDeniedFilesList("exe,bat,jsp,js,htm,html");
  upload.upload();
  upload.save("/images");

  String p_id=upload.getRequest().getParameter("id");
  String p_name=upload.getRequest().getParameter("name");
 int st_id=Integer.parseInt(upload.getRequest().getParameter("type"));
  double p_price=Double.valueOf(upload.getRequest().getParameter("price"));
  int p_quantity=Integer.parseInt(upload.getRequest().getParameter("quantity"));  
  String filename1=upload.getFiles().getFile(0).getFileName();
   String filename2=upload.getFiles().getFile(1).getFileName();
   String filename3=upload.getFiles().getFile(2).getFileName();
      if(filename1.equals("")||filename1==null){
filename1="default.jpg";
}
   System.out.println();
   System.out.println(filename1);
if(filename2.equals("")||filename2==null){
filename2="default.jpg";
}
   System.out.println();
   System.out.println(filename2);
if(filename3.equals("")||filename3==null){
filename3="default.jpg";
}
   
   
   
   
   
   
   
   String filename=filename1+";"+filename2+";"+filename3;
  String p_description=upload.getRequest().getParameter("description");
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格
  String date=df.format(new Date());// new Date()为获取当前系统时间
 
db.OpenConn();
db.createStmt();
    String sql1="select * from product where p_name="+"'"+p_name+"'";
  ResultSet rs=db.Query(sql1);
  
 if(rs.next()){
  %>
商品名已存在，请重新输入！等待3秒后返回。
 <%
 response.setHeader("Refresh","3;URL=addproduct.jsp");
 rs.close();
 } 
  else{



 
 
  
   String sql="insert into product(p_id,p_name,st_id,p_price,p_quantity,p_image,p_description,p_time) values('"+p_id+"','"+p_name+"',"+st_id+","+p_price+","+p_quantity+",'"+filename+"','"+p_description+"','"+date+"')";//定义插入SQL语句
   System.out.println(sql);


int result=db.Update(sql);

 if(result==1){ 
 
%>  插入成功，等待2秒后返回。<% 
response.setHeader("Refresh","2;URL=addproduct.jsp");//执行插入成功跳转
 }else{
 %>
 插入失败，等待2秒后返回。
 <%
 response.setHeader("Refresh","2;URL=addproduct.jsp");//执行插入失败跳转
 }
 db.closeStmt();
 db.closeConn();
}%>  

  </body>

</html>
